<template>
    <div>
        <el-dialog v-model="props.visible" title="生产单列表" width="80%" @close="handleCancel">
            <el-form :model="queryParams" class="flex-box" ref="queryRef" :inline="true">
                <div>
                    <el-form-item label="生产单编码" prop="produceNo">
                        <el-input
                            v-model.trim="queryParams.produceNo"
                            placeholder="请输入生产单编码"
                            @keyup.enter="handleQuery"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">
                            搜索
                        </el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </div>
                <el-button type="primary" plain @click="handleSubmit" :disabled="multiple">
                    确认
                </el-button>
            </el-form>
            <el-row :gutter="20" style="overflow: auto">
                <el-col :span="8">
                    <el-table
                        v-loading="loading"
                        :data="orderList"
                        stripe
                        border
                        style="cursor: pointer"
                        @cell-click="handleClick"
                        ref="orderListTable"
                        highlight-current-row
                    >
                        <el-table-column label="序号" width="55" type="index" align="center" />
                        <el-table-column
                            label="生产单编码"
                            align="center"
                            prop="produceNo"
                        ></el-table-column>
                        <el-table-column
                            label="下单日期"
                            align="center"
                            prop="submitDate"
                        >
                            <template v-slot="{ row }">
                                {{ row.submitDate ? row.submitDate.split('T')[0] : '' }}
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="类型"
                            align="center"
                            prop="type"
                        >
                            <template v-slot="{ row }">
                                <el-tag :type="row.type == 1 ? 'success' : ''" effect="dark">
                                    {{ row.type == 1 ? '封面' : '内文' }}
                                </el-tag>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
                <el-col :span="16">
                    <el-table
                        ref="orderRef"
                        v-loading="loadingInfo"
                        :data="bindList"
                        row-key="pieceId"
                        stripe
                        border
                        style="cursor: pointer"
                        height="420px"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" :reserve-selection="true" width="55" />
                        <el-table-column
              label="序号"
              width="55"
              type="index"
              align="center"
            />
            <el-table-column
              label="书刊名称"
              min-width="200"
              align="center"
              prop="bookName"
            ></el-table-column>
            <el-table-column
              label="品种"
              align="center"
              prop="breedType"
            ></el-table-column>

            <el-table-column
              label="书刊编码"
              align="center"
              prop="bookNo"
            ></el-table-column>
            <el-table-column
              label="贴标"
              align="center"
              prop="pieceView"
            ></el-table-column>
            <el-table-column label="印刷方式" align="center" prop="printType">
              <template #default="scope">
                <dict-tag
                  :options="mes_paper_package"
                  :value="scope.row.printType"
                />
              </template>
            </el-table-column>
            <el-table-column label="印色" align="center" prop="printColor">
              <template #default="scope">
                <dict-tag
                  :options="ems_order_print_color"
                  :value="scope.row.printColor"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="印张"
              align="center"
              prop="sheet"
            ></el-table-column>

            <el-table-column
              label="印数"
              align="center"
              prop="printAmount"
            ></el-table-column>
          </el-table>
                </el-col>
            </el-row>
            <div class="footer">
                <pagination
                    v-show="total > 0"
                    :total="total"
                    v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize"
                    @pagination="getList"
                />
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { toNumber } from '@/utils/ruoyi'
import {
    getProduceOrderList,
    getProduceById,
    GetProducelistByMachineId,
    getByIdFilter
} from '@/api/production/cover'
const emit = defineEmits(['changeUpdate', 'cancel'])
const { proxy } = getCurrentInstance()
const {
    mes_paper_package,
    mes_paper_specs,
    ems_order_print_color,
    ems_produce_book_size,
    ems_order_binding_mode,
} = proxy.useDict(
    'mes_paper_package',
    'ems_order_print_color',
    'ems_produce_book_size',
    'ems_order_binding_mode',
    'mes_paper_specs'
)

const props = defineProps({
    visible: {
        type: Boolean,
        default: false,
    },
    orderSelectList: {
        type: Array,
        default: () => {
            ;[]
        },
    },
    params: {
        type: Object,
        default: () => {
            {
            }
        },
    },
})

const orderRef = ref(null)
const orderList = ref([])
const bindList = ref([])
const multiple = ref(true)
const selectRow = ref([])
const loading = ref(false)
const loadingInfo = ref(false)
const orderListTable = ref(null)
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    type: 7, //1 实际覆膜是7
    status: '5,6,7,8,9,10,11,12,13,14,15,16,17,18',
})
watch(
    () => props.visible,
    val => {
        if (val) {
            handleQuery()
            nextTick(() => {
                orderRef.value.clearSelection()
                if (props.orderSelectList.length > 0) {
                    props.orderSelectList.forEach(item => {
                        orderRef.value.toggleRowSelection(item, true)
                    })
                }
            })
        }
    }
)
const total = ref(0)
// 选中订单
const handleClick = row => {
    loadingInfo.value = true
    getByIdFilter({ produceId: row.produceId ,machineId: props.params.machineId}).then(res => {
        res.data.producePieceList.forEach(item => {
            item.produceNo = res.data.produceNo  //row.produceNo
            item.submitDate = res.data.submitDate  //row.submitDate
            item.type = res.data.type
            res.data.producePrintList.forEach(ele => {
                if (item.printId == ele.printId) {
                    // 覆膜张数 = 印数
                    item.coveringPageNum = ele.printAmount
                    item.specification = ele.sheetSize
                    res.data.producePaperList.forEach(paepr => {
                        if (ele.paperNo == paepr.paperNo) {
                            item.paperName = paepr.paperName
                            item.paperType = paepr.paperType
                            item.weight = paepr.weight
                            //item.specification = paepr.specifications

                        }
                    })
                    // 最大上报量 = 印数+ 另行加放
                    // item.maxNum = Number(ele.printAmount) + Number(ele.lxjf)
                    res.data.produceProductList.forEach(obj => {
                        if (ele.orderProductId === obj.orderProductId) {
                            item.printColor = obj.printColor
                            item.bookSize = obj.bookSize
                            item.composition = obj.composition
                            item.breed = obj.breed
                            item.finishSize = obj.finishSize
                            item.bookSize = obj.bookSize
                            item.bindingMode = obj.bindingMode
                        }
                    })
                    item.lossCoefficient = ele.lossCoefficient // 损耗系数
                    item.lxjf=ele.lxjf //另行加放
                    item.bookName = ele.bookName
                    item.breedType = ele.breed;
                    item.bookNo = ele.bookNo
                    item.printAmount = ele.printAmount
                    item.totalNum=item.totalNum //累计上报量
                    item.maxNum=item.maxNum //本次最大允许上报量
                }
            })
        })

        bindList.value = res.data.producePieceList //produceCoveringList
        // bindList.value[0].pieceId = res.data.producePieceList[0]?.pieceId
        loadingInfo.value = false
    })
}

// 选中
const handleSelectionChange = row => {
    selectRow.value = row
    multiple.value = !row.length
}
const handleSubmit = () => {
    emit('changeUpdate', selectRow.value)
}
const handleCancel = () => {
    bindList.value = []
    proxy.resetForm('queryRef')
    emit('cancel')
}
const getList = async () => {
    loading.value = true
    const res = await GetProducelistByMachineId({ ...queryParams.value, ...props.params })
    orderListTable.value.setCurrentRow(res.rows[0])
    loading.value = false
    orderList.value = res.rows
    if (orderList.value.length > 0) {
        handleClick(orderList.value[0])
    } else {
        bindList.value = []
    }
    total.value = res.total
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1
    getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm('queryRef')
    handleQuery()
}

// getList()
</script>

<style lang="scss" scoped></style>
